
path = strcat(datapath,'train.dat');
base = load(path);
alldata = base(:,1:3);
alldata(:,1) = alldata(:,1)+1;
alldata(:,2) = alldata(:,2)+1;
clear base
%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%
%          important :                %
% userId and ItemId is from 0, not 1  %
%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!%
num_rates = size(alldata,1);

rate_matrix_double_user = zeros(num_users, num_items*5);
rate_matrix_double_item = zeros(num_items, num_users*5);
rate_matrix_user = boolean(rate_matrix_double_user);
rate_matrix_item = boolean(rate_matrix_double_item);
rate_matrix_template_user = boolean(rate_matrix_double_user);
rate_matrix_template_item = boolean(rate_matrix_double_item);
clear rate_matrix_double_user
clear rate_matrix_double_item

tmp_index = 0;
for m=1:num_rates
    if mod(m,10000)==0
        fprintf(1,'%4.9f\n',m/num_rates);
    end
    tmp_index = alldata(m,2)*5-5;
    rate_matrix_user(alldata(m,1), tmp_index+alldata(m,3)) = 1;
    rate_matrix_template_user(alldata(m,1), tmp_index+1:tmp_index+5) = 1;
    tmp_index = alldata(m,1)*5-5; 
    rate_matrix_item(alldata(m,2), tmp_index+alldata(m,3)) = 1;
    rate_matrix_template_item(alldata(m,2), tmp_index+1:tmp_index+5) = 1;
end

sum(sum(rate_matrix_user))
sum(sum(rate_matrix_item))


testpath =strcat(datapath, 'test.dat');
test = load(testpath);
test_real = test(:,1:3);
test_real(:,1) = test_real(:,1)+1;
test_real(:,2) = test_real(:,2)+1;

clear test
test_size = size(test_real,1);

test_real_matrix = zeros(num_users, num_items);
for m=1:size(test_real,1)
    test_real_matrix(test_real(m,1),test_real(m,2)) = test_real(m,3);
end
test_template = test_real_matrix>0;




